package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.IndexImg;
import com.powernode.domain.Notice;
import com.powernode.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop/notice")
@Api(tags = "门店公告")
public class ShopNoticeController {

    @Autowired
    private NoticeService noticeService;


    @GetMapping("/page")
    @ApiOperation(value = "公告管理分页")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public ResponseEntity<Page<Notice>> noticePage(Page<Notice> page, Notice notice) {
        page = noticeService.noticePage(page, notice);
        return ResponseEntity.ok(page);
    }

    @PostMapping
    @ApiOperation(value = "公告新增")
    @PreAuthorize("hasAuthority('shop:notice:save')")
    public ResponseEntity<Void> addIndexImg(@RequestBody Notice notice) {
        noticeService.save(notice);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/topNoticeList")
    @ApiOperation(value = "小程序首页公共通知")
    public ResponseEntity<List<Notice>> topNoticeList() {
        List<Notice> list = noticeService.list();
        return ResponseEntity.ok(list);
    }


    @GetMapping("/noticeList")
    @ApiOperation(value = "小程序最新公告")
    public ResponseEntity<List<Notice>> noticeList() {
        List<Notice> list = noticeService.list(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getIsTop,1)
                .eq(Notice::getStatus,1));
        return ResponseEntity.ok(list);
    }
}
